clear
global OUT "${results}"
local var var
local ltfpvar tfpvar
local ii=1
	local sample all

	use "${results}/temp/results${date}.dta" if sample=="`sample'", clear
	foreach svar in tau capT{
		local var_`svar'_`sample'=var_`svar'[1]
		local printvar_`svar'_`sample' = string(`var_`svar'_`sample'', "%05.3f")
		local perm_var_`svar'_mean_`sample'=perm_var_`svar'_mean[1]
		local printperm_var_`svar'_mean_`sample' = string(`perm_var_`svar'_mean_`sample'', "%05.3f")
	}
	foreach svar in tau capT{
		use "${results}/temp/results_cov_${date}.dta" if sample=="`sample'",clear
		local mean_cov_`svar'_`sample'=mean_cov_`svar'[1]
		local printmean_cov_`svar'_`sample'= string(`mean_cov_`svar'_`sample'', "%05.3f")
		local bRPC_`svar'_`sample' 	= sqrt(`mean_cov_`svar'_`sample''/`var_`svar'_`sample'')
	}

foreach svar in capT tau{

	graph use "${results}/temp/kden_temp_`svar'_`ltfpvar'_all${date}.gph"
	clear
	serset use 
	ren _all (d_`svar'_`ltfpvar'_true x_`svar'_`ltfpvar'_true discard)
	drop discard
	drop if d_`svar'_`ltfpvar'_true==.
	
	*Old Mean for New Distribution
	sum x_`svar'_`ltfpvar'_true
	local mu_X = 0

	*New Locationnfor New Distribution
	local aGBAdj 	= ( 1 - `bRPC_`svar'_all')*`mu_X'

	*New Distribution Points
	gen x_GBAdj	= x_`svar'_`ltfpvar'_true*`bRPC_`svar'_`sample'' + `aGBAdj'
	gen d_GBAdj  	= d_`svar'_`ltfpvar'_true/(`bRPC_`svar'_`sample'')

		
	tempfile temp1
	save `temp1'
	use "${results}/temp/permute_kdenspoints_`sample'_prodvar`ii'_optimal_`svar'_`var'_${date}.dta", clear
	merge 1:1 _n using `temp1'
	tempfile graph`svar'
	save `graph`svar''
}
	



local svar capT
use `graph`svar''

twoway ///
(line d_`svar'_`ltfpvar'_true x_`svar'_`ltfpvar'_tr, lcolor(black) lpattern(solid)) ///
(line `svar'_d `svar'_x if `svar'_x<p95_`svar'[1] & `svar'_x>p5_`svar'[1], lcolor(red) lpattern(dash_dot)) ///
(line d_GBAdj x_GBAdj, lcolor(blue) lpattern(longdash)) , ///
graphregion(color(white)) bgcolor(white) plotregion(style(none)) ///
xscale(range(-.5 .5)) yscale(range(0 7.25)) ///
xlabel(-.4(.2).4,nogrid ) ylabel(0(2)6.25,nogrid ) ///
ytitle("Density") xtitle("Location Effect") ///
legend( lab(1 "                                     " ) label(2 " " " " " ") label(3 " " "" ) label(4 " " " ") position(2) ring(0) cols(1) region(lwidth(none) lcolor(none) color(none)) symxsize(*.5) )  ///
text([withheld] [withheld]   "Empirical:                                            " "Raw Var(`=ustrunescape("\u03BE\u0302")'{sub:l}) =  `printvar_`svar'_all'", justification(left) ) ///
text([withheld] [withheld]  "Permutation:                                       " "Mean Var(`=ustrunescape("\u03BE\u0302")'{sub:l}) = `printperm_var_`svar'_mean_all'", justification(left) color(red)) ///
text([withheld] [withheld]  "Bias-Corrected:                                  " "Cov(`=ustrunescape("\u03BE\u0302")'{sub:l}{sup:A},`=ustrunescape("\u03BE\u0302")'{sub:l}{sup:B}) = Var(`=ustrunescape("\u03BE")'{sub:l}) = `printmean_cov_`svar'_all'", justification(left) color(blue) ) 
graph export "$OUT/Fig1a.png", replace

local svar tau
use `graph`svar''

twoway ///
(line d_`svar'_`ltfpvar'_true x_`svar'_`ltfpvar'_tr, lcolor(black) lpattern(solid)) ///
(line `svar'_d `svar'_x if `svar'_x<p95_`svar'[1] & `svar'_x>p5_`svar'[1], lcolor(red) lpattern(dash_dot)) ///
(line d_GBAdj x_GBAdj, lcolor(blue) lpattern(longdash)) , ///
graphregion(color(white)) bgcolor(white) plotregion(style(none)) ///
xscale(range(-.5 .5)) yscale(range(0 7.25)) ///
xlabel(-.4(.2).4,nogrid ) ylabel(0(2)6.25,nogrid ) ///
ytitle("Density") xtitle("Industry-Specific Location Effect") ///
legend( lab(1 "                                       " ) label(2 " " " " " ") label(3 " " "" ) label(4 " " " ") position(2) ring(0) cols(1) region(lwidth(none) lcolor(none) color(none)) symxsize(*.5) )  ///
text([withheld] [withheld] "Empirical:                                                  " "Raw Var(`=ustrunescape("\u03C4\u0302")'{sub:l,i}) =  `printvar_`svar'_all'", justification(left)) ///
text([withheld] [withheld] "Permutation:                                             " "Mean Var(`=ustrunescape("\u03C4\u0302")'{sub:l,i}) = `printperm_var_`svar'_mean_all'", color(red) justification(left)) ///
text([withheld] [withheld] "Bias-Corrected:                                        " "Cov(`=ustrunescape("\u03C4\u0302")'{sub:l,i}{sup:A},`=ustrunescape("\u03C4\u0302")'{sub:l,i}{sup:B}) = Var(`=ustrunescape("\u03C4")'{sub:l,i}) = `printmean_cov_`svar'_all'", color(blue) justification(left)) 

graph export "$OUT/Fig1b.png", replace
